﻿using System;
using System.Collections.Generic;
using System.Text;
using FunctionalCLR.Core;
using FunctionalCLR.Shell;
using DhbMatrixAlgebra;

namespace Zove.Shell.Core.Function.Matrix {
    public class SolveLinearNode : ContextFuncNode {

        public SolveLinearNode()
            : base() {
            base.Value = "solve";
        }

        public override object Evaluate() {
            int length = Parameters.Count;
            if (length < 2)
            {
                throw new ArgumentException(
                    "usage: solve(matrix, n)");
            }

            double[,] m = null;
            double[] n = null;

            List<object> mat = Parameters[length-1].Evaluate() as List<object>;
            m = MatrixUtils.GetMatrix(mat);

            mat = Parameters[length-2].Evaluate() as List<object>;
            n = MatrixUtils.GetArray(mat);

            LinearEquations le = new LinearEquations(m, n);
            DhbVector vector = le.Solution();
            for (int i = 0; i < n.Length; i++) {
                n[i] = vector[i];
            }

            return n;
        }
    }
}
